package org.example.classTest;

import org.example.io.BinaryFileUtils;

import java.io.IOException;

/**
 * @description:
 * @author: farprt
 * @create: 2022-08-16 00:07
 **/
public class MyClassLoader extends ClassLoader{
    private static final String BASE_DIR= "/";

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String fileName=name.replaceAll("\\.","/");
        fileName=BASE_DIR+fileName+".class";
        try {
            byte[] bytes=BinaryFileUtils.readFileToByteArray(fileName);
            return defineClass(name,bytes,0,bytes.length);
        } catch (IOException e) {
            throw new ClassNotFoundException("fail to load class"+name,e);
        }
    }


}